package com.beteng.su.betengsu.utils;

import android.app.Activity;
import android.graphics.Bitmap;
import android.util.Base64;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @项目名: Beteng
 * @包名: com.beteng.util
 */
public class FileUtils {

    public static final String DOWNLOAD_DIR = "download";
    public static final String CACHE_DIR = "cache";
    public static final String ICON_DIR = "icon";
    private static final String TAG = "FileUtils";
    public static final String FILLE_NAME = "Fille";
    public static final String FILE_EXTENSION = "FileExtension";




    public static JSONArray getIVJsonFile(Activity activity, File... files) {

        if (files.length==0) {
            return null;
        }

        JSONArray array = new JSONArray();
        for (File file : files) {
            Map ivData = getSingleIvData(activity,file);
            JSONObject object = new JSONObject(ivData);
            array.put(object);
        }

        return array;
    }

    public static JSONArray getIVJsonFile(Activity activity, List<String> list) {

        List<File> files = new ArrayList<>();
        for (String str:list){
            File file = new File(str);
            files.add(file);
        }


        JSONArray array = new JSONArray();
        for (File file : files) {
            Map ivData = getSingleIvData(activity,file);
            JSONObject object = new JSONObject(ivData);
            array.put(object);
        }

        return array;
    }

    public static Map getSingleIvData(Activity activity, File file) {

        String base64Str;
        final String fileName=file.getName();
        String prefix=fileName.substring(fileName.lastIndexOf(".")+1);

        Bitmap bitmap = BitmapHelper.compressImageFromFile(file.toString());
        byte[] byteFromBitmap = BitmapHelper.getByteFromBitmap(bitmap);
        base64Str = Base64.encodeToString(byteFromBitmap, Base64.DEFAULT);

        Log.e("FileUtils", "byteFromBitmap:" + byteFromBitmap.length);

        Map<String,String> map = new HashMap<>();
        map.put(FILLE_NAME, base64Str);
        map.put(FILE_EXTENSION,prefix);
        return map;

    }
}
